<!--- ==================================================
NAME:	crt_order.cfm
DESC:	This is the file that creates an order.
=================================================== --->
<cfsetting enablecfoutputonly="yes">

<!--- default data --->
<cfset var.start = CreateODBCdateTime(now())>
<cfparam name="arguments.url" default="#application.xtiurl#">
<cfparam name="arguments.dtd" default="#application.xtidtd#">
<cfparam name="arguments.debug" default="false">
<cfparam name="arguments.debuglevel" default="1,2,3">
<cfparam name="arguments.customerid">
<cfparam name="arguments.contactid" default="">
<cfparam name="arguments.crt_order" default="">
<cfparam name="arguments.order_number" default="">
<cfparam name="arguments.misc_code_1" default="">
<cfparam name="arguments.misc_amt_1" default="">
<cfparam name="arguments.misc_code_2" default="">
<cfparam name="arguments.misc_amt_2" default="">
<cfparam name="arguments.source">
<cfparam name="arguments.ship_complete" default="">
<cfparam name="arguments.tsr_taker" type="any" default="">
<cfparam name="arguments.user" default="web">
<cfparam name="arguments.locid">
<cfparam name="arguments.cid" default="">
<cfparam name="arguments.client_order" default="">
<cfparam name="arguments.order_type" default="R">
<cfparam name="arguments.program_id" default="">
<cfparam name="arguments.confidence" default="">
<cfparam name="arguments.close_date" default="">
<cfparam name="arguments.close_amt" default="">
<cfparam name="arguments.currency" default="USD">

<cfparam name="arguments.card_number" default="">
<cfparam name="arguments.security_code" default="">
<cfparam name="arguments.expr_date" default="">

<cfparam name="arguments.payment_type" default="">
<!--- <cfswitch expression="#LCase(arguments.payment_type)#">
	<cfcase value="v">
		<cfset arguments.payment_type = "VISA">
	</cfcase>
	<cfcase value="m">
		<cfset arguments.payment_type = "MC">
	</cfcase>
	<cfcase value="d">
		<cfset arguments.payment_type = "DC">
	</cfcase>
	<cfdefaultcase>
		<cfset arguments.payment_type = "">
	</cfdefaultcase>
</cfswitch> --->

<!--- optional amex procurement id --->
<cfparam name="arguments.amex_num" default="">

<cfparam name="arguments.ponumber" default="">
<cfparam name="arguments.pay_type" default="V">
<cfparam name="arguments.array_ccs_new" default="">
<cfparam name="arguments.array_ccs_orig" default="">
<cfparam name="arguments.company_name">
<cfparam name="arguments.attention">
<cfparam name="arguments.address1">
<cfparam name="arguments.address2" default="">
<cfparam name="arguments.address3" default="">
<cfparam name="arguments.address4" default="">
<cfparam name="arguments.city">
<cfparam name="arguments.state">
<cfparam name="arguments.country" type="any" default="">
<cfparam name="arguments.zip" default="">
<cfparam name="arguments.geo_code" default="">
<cfparam name="arguments.phone">
<cfparam name="arguments.code">
<cfparam name="arguments.account_number" default="">
<cfparam name="arguments.ship_price" default="">
<cfparam name="arguments.ship_code_reason" default="">
<cfparam name="arguments.cqsd" default="">
<cfparam name="arguments.packing_slip_notes" default="">
<cfparam name="arguments.invoice_notes" default="">
<cfparam name="arguments.management_notes" default="">
<cfparam name="arguments.auto_bo" default="">
<cfparam name="arguments.future_ship" default="">
<cfparam name="arguments.array_parts" type="any" default="">
<cfparam name="arguments.array_systems" type="any" default="">
<cfparam name="arguments.last_four_ssn" type="any" default="">
<cfparam name="arguments.date_of_birth" type="any" default="">
<cfparam name="arguments.promotional_financing" type="any" default="">
<cfparam name="arguments.ship_to_customer" type="any" default="">
<cfparam name="arguments.validate_po" default="">

<!---
	<!--- include the encryption module to for encrypting/decrypting credit card data
		Functions included: 
			• sap_encrypt(originalData) == returns the encrypted string data
			• sap_decrypt(encryptedData) == returns the decrypted string data (original data)
	---><cfinclude template="sap_encryption.cfm">
--->

<!--- If a credit card was used then determine the payment_type based on the pay_type 
<cfif arguments.payment_type EQ "CC">
	<cfswitch expression="#arguments.pay_type#">
		<cfcase value="V">
			<cfset arguments.payment_type = "VISA">
		</cfcase>
		<cfcase value="M">
			<cfset arguments.payment_type = "MC">
		</cfcase>
		<cfcase value="D">
			<cfset arguments.payment_type = "DC">
		</cfcase>
		<cfcase value="A">
			<cfset arguments.payment_type = "AMEX">
		</cfcase>
		<cfdefaultcase>
			<cfset arguments.payment_type = "">
		</cfdefaultcase>
	</cfswitch>
</cfif>--->

<!--- Defect 1821 - Address Verification is required for all create and change order posts --->
<cfquery name="verify_address" datasource="#application.dsn#">
	asp_invoice_number_get_by_customer_and_address
		@iCustomer_Number = #arguments.customerid#,
		@iAddress_1 = '#arguments.address1#',
		@iAddress_2 = <cfif len(trim(arguments.address2))>'#arguments.address2#'<cfelse>null</cfif>,
		@iCity = '#arguments.city#',
		@iState_Code = '#arguments.state#',
		@iPostal_Code = #arguments.zip#
</cfquery>
<cfif verify_address.recordcount gte 1>
	<cfset arguments.address_verified = "Y">
<cfelse>
	<cfset arguments.address_verified = "N">
</cfif>



<!--- build xml statement --->
<cfscript>
/* MultiCC */
payment = "";
if  (isarray(arguments.array_ccs_orig) and arraylen(arguments.array_ccs_orig) GT 0) {
	indexcnt_orig = arraylen(arguments.array_ccs_orig) + 1;
	for ( i=1; i lt indexcnt_orig ; i=i+1) {
		
		var.sap_encryption_util = createObject("java","UtilEncrypt");
		var.sap_encryption_key = application.sap_encryption_key;
		
		var.sap_encryption_originalData_ccnum = arguments.array_ccs_orig[i].cc_num;
		var.card_type = application.string_validation.getCardType(var.sap_encryption_originalData_ccnum);
		arguments.payment_type = arguments.array_ccs_orig[i].cc_type;
		//if the cc_type was passed, then set it
		if (arguments.array_ccs_orig[i].cc_type NEQ "") {
			arguments.payment_type = arguments.array_ccs_orig[i].cc_type;
		}
		//else if find out what kind of card type it is
		else if(IsStruct(var.card_type) AND StructKeyExists(var.card_type,"card_type_abbr") AND Len(var.card_type.card_type_abbr) GT 0){
			arguments.payment_type = var.card_type.card_type_abbr; }
		//else default it
		else {
			// do not replace arguments.payment_type variable when no card type could be determined for this card entry.			
			if(Len(arguments.pay_type) GT 0){
				arguments.payment_type = arguments.pay_type;
			}
		}
		var.sap_encryption_encodeEncryptData_ccnum = var.sap_encryption_util.encrypt( var.sap_encryption_key, var.sap_encryption_originalData_ccnum, "");
		
		var.sap_encryption_originalData_sec = arguments.array_ccs_orig[i].cc_security;
		var.sap_encryption_encodeEncryptData_sec = var.sap_encryption_util.encrypt( var.sap_encryption_key, var.sap_encryption_originalData_sec, "");
		
		payment = payment & '		 
		<payment type="#arguments.payment_type#">			
			<card_number>#var.sap_encryption_encodeEncryptData_ccnum#</card_number>
			<expr_date>#Ucase(arguments.array_ccs_orig[i].cc_exp)#</expr_date>
			<po_number>#Ucase(arguments.ponumber)#</po_number>
			<amount>#Ucase(arguments.array_ccs_orig[i].cc_amt)#</amount>
			<security_code>#var.sap_encryption_encodeEncryptData_sec#</security_code>
		</payment>';
	}
}

new_payment="";
if  (isarray(arguments.array_ccs_new) and arraylen(arguments.array_ccs_new) GT 0) {
	indexcnt_new = arraylen(arguments.array_ccs_new) + 1;
	for ( j=1; j lt indexcnt_new ; j=j+1) {				
		var.sap_encryption_util = createObject("java","UtilEncrypt");
		var.sap_encryption_key = application.sap_encryption_key;
		
		var.sap_encryption_originalData_ccnum = arguments.array_ccs_new[j].cc_num;
		var.card_type = application.string_validation.getCardType(var.sap_encryption_originalData_ccnum);
		//if the cc_type was passed, then set it
		if (StructKeyExists(arguments.array_ccs_new[j],"cc_type") and arguments.array_ccs_new[j].cc_type NEQ "") {
			arguments.payment_type = arguments.array_ccs_new[j].cc_type;
		}
		//else if find out what kind of card type it is
		else if(IsStruct(var.card_type) AND StructKeyExists(var.card_type,"card_type_abbr") AND Len(var.card_type.card_type_abbr) GT 0){
			arguments.payment_type = var.card_type.card_type_abbr; }
		//else default it
		else {
			// do not replace arguments.payment_type variable when no card type could be determined for this card entry.			
			if(Len(arguments.pay_type) GT 0){
				arguments.payment_type = arguments.pay_type;
			}
		}
		var.sap_encryption_encodeEncryptData_ccnum = var.sap_encryption_util.encrypt( var.sap_encryption_key, var.sap_encryption_originalData_ccnum, "");
		
		var.sap_encryption_originalData_sec = arguments.array_ccs_new[j].cc_security;
		var.sap_encryption_encodeEncryptData_sec = var.sap_encryption_util.encrypt( var.sap_encryption_key, var.sap_encryption_originalData_sec, "");
		
	  	new_payment = new_payment & '
		<payment type="#arguments.payment_type#">
			<card_number>#var.sap_encryption_encodeEncryptData_ccnum#</card_number>
			<expr_date>#Ucase(arguments.array_ccs_new[j].cc_exp)#</expr_date>
			<amount>#Replace(arguments.array_ccs_new[j].cc_amt, ',', '', 'ALL')#</amount>
			<security_code>#var.sap_encryption_encodeEncryptData_sec#</security_code>
			<last_four_ssn>#arguments.last_four_ssn#</last_four_ssn>
			<date_of_birth>#arguments.date_of_birth#</date_of_birth>
			<promotional_financing>#arguments.promotional_financing#</promotional_financing>
		</payment>';
		
	}
}

cart = "";
subtotal = 0;
// Start with standalone parts
if  (isarray(arguments.array_parts) and arraylen(arguments.array_parts) GT 0) {
	indexcnt = arraylen(arguments.array_parts) + 1;
	for ( i=1; i lt indexcnt ; i=i+1) {
		cart = cart & '
			<line_item>
				<product>				
					<product_code>#Ucase(arguments.array_parts[i].product)#</product_code>';
				if (arguments.array_parts[i].type IS "original"){
					cart = cart & '
					<item_number>#Ucase(arguments.array_parts[i].line)#</item_number>';
					}
				cart = cart & '
					<quantity>#Ucase(arguments.array_parts[i].quantity)#</quantity>
					<price>#Ucase(arguments.array_parts[i].price)#</price>';
					if (structKeyExists(arguments.array_parts[i],'promo_code_1')) {
						cart = cart & '<promo_code_1>#Ucase(arguments.array_parts[i].promo_code_1)#</promo_code_1>';
					}
					if (structKeyExists(arguments.array_parts[i],'promo_code_2')) {
						cart = cart & '<promo_code_2>#Ucase(arguments.array_parts[i].promo_code_2)#</promo_code_2>';
					}
					cart = cart & '
					<rsn_code_prc>#Ucase(arguments.array_parts[i].price_reason_code)#</rsn_code_prc>';
					if (structKeyExists(arguments.array_parts[i],'weight')) {
						cart = cart & '<weight>#application.string_validation.YNFormat(arguments.array_parts[i].weight)#</weight>';
					}				
					if (structKeyExists(arguments.array_parts[i],'condition_type')) {
						cart = cart & '<condition_type>#arguments.array_parts[i].condition_type#</condition_type>';
					}	
					cart = cart & '										
					<new_line>N</new_line>
				</product>
			</line_item>';
	}
}

// Now the systems 
if  (isarray(arguments.array_systems) and arraylen(arguments.array_systems)) {
	indexcnt1 = arraylen(arguments.array_systems) + 1;  
	for (x=1; x lt indexcnt1; x=x+1) {
	cart = cart & '
			<line_item>	
				<configuration>';
					if (arguments.array_systems[x].type IS "original"){
					cart = cart & '
					<ln>#Ucase(arguments.array_systems[x].line)#</ln>';
					}
					cart = cart & '
					<quantity>#Ucase(arguments.array_systems[x].quantity)#</quantity>
					<baseunit>
						<product_code>#Ucase(arguments.array_systems[x].b_product)#</product_code>
						<price>#Ucase(arguments.array_systems[x].b_price)#</price>
						<promo_code_1>#Ucase(arguments.array_parts[i].promo_code_1)#</promo_code_1>
						<promo_code_2>#Ucase(arguments.array_parts[i].promo_code_2)#</promo_code_2>
						<rsn_code_prc>#Ucase(arguments.array_systems[x].price_reason_code)#</rsn_code_prc>
					</baseunit>';
					indexcnt2 = arraylen(arguments.array_systems[x].subitem) + 1;
					for (y=1; y lt indexcnt2; y=y+1) {
					cart = cart & '
						<component>
							 <product_code>#Ucase(arguments.array_systems[x].subitem[y].product)#</product_code>
							 <quantity>#Ucase(arguments.array_systems[x].subitem[y].quantity)#</quantity>
							 <price>#Ucase(arguments.array_systems[x].subitem[y].price)#</price>
							 <rsn_code_prc>#Ucase(arguments.array_systems[x].subitem[y].price_reason_code)#</rsn_code_prc>
						</component>';
					}
		cart = cart & '
				 </configuration>
			</line_item>';
		 }	
} 

// Now build the rest of the xml to post to middle ware. KEEP FORMATING SAME AS BELOW...
xi_namespace='#trim(application.xi_namespace)#:ORDERS:ChangeOrder';
xi_interface='ChangeOrder_OB';
xml = '<xtiinput>
	<inputheader>
		<actionset>
			<action>chg_order</action>
		</actionset>
	</inputheader>
	<inputdata>
		<system>#variables.system#</system>
		<edit_type>#arguments.edit_type#</edit_type>
		<customerid>#Ucase(arguments.customerid)#</customerid>
		<shipto_customerid>#Ucase(arguments.ship_to_customer)#</shipto_customerid>
		<contactid>#Ucase(arguments.contactid)#</contactid>
		<order_number>#Ucase(arguments.order_number)#</order_number>
		<validate_po>#ucase(arguments.validate_po)#</validate_po>
		<order_header>
			<source>#Ucase(arguments.source)#</source>
			<ship_complete>#Ucase(arguments.ship_complete)#</ship_complete>
			<tsr_taker>#Ucase(arguments.tsr_taker)#</tsr_taker>
			<client_order>#Ucase(arguments.ponumber)#</client_order>
			<order_type>#XmlFormat(Ucase(arguments.order_type))#</order_type>
			<locid>#arguments.locid#</locid>
			<cid>#arguments.cid#</cid>
			<pay_type>#arguments.pay_type#</pay_type>
			<confidence>#arguments.confidence#</confidence>
			<future_ship_date>#Ucase(arguments.future_ship)#</future_ship_date>
			<shipment_type>#ucase(arguments.auto_bo)#</shipment_type>
			<currency>#trim(arguments.currency)#</currency>
		</order_header>
		#payment#
		#new_payment#
		<shipping_addr>
			<company_name>#replace(XmlFormat(Ucase(arguments.company_name)), '&amp;AMP;', '&amp;', 'all')#</company_name>
			<attention>#replace(XmlFormat(Ucase(arguments.attention)), '&amp;AMP;', '&amp;', 'all')#</attention>
			<address_1>#replace(XmlFormat(Ucase(arguments.address1)), '&amp;AMP;', '&amp;', 'all')#</address_1>
			<address_2>#replace(XmlFormat(Ucase(arguments.address2)), '&amp;AMP;', '&amp;', 'all')#</address_2>
			<city>#Ucase(arguments.city)#</city>
			<state>#Ucase(arguments.state)#</state>
			<country>#Ucase(arguments.country)#</country>
			<zip>#Ucase(arguments.zip)#</zip>
			<geo_code>#arguments.geo_code#</geo_code>
			<phone>#Ucase(arguments.phone)#</phone>
			<addr_verified>#arguments.address_verified#</addr_verified>
		</shipping_addr>
		<shipping_codes>
			<method>
				<code>#Ucase(arguments.code)#</code>
				<account_number>#Ucase(arguments.account_number)#</account_number>
				<price>#Ucase(arguments.ship_price)#</price>
				<rsn_code_ship>#Ucase(trim(arguments.ship_code_reason))#</rsn_code_ship>
			</method>
		</shipping_codes>
		<system_notes>
			<packing_slip_notes>#XmlFormat(Ucase(arguments.packing_slip_notes))#</packing_slip_notes>
			<invoice_notes>#XmlFormat(Ucase(arguments.invoice_notes))#</invoice_notes>
			<management_notes>#XmlFormat(Ucase(arguments.management_notes))#</management_notes>
		</system_notes>
		<shopping_cart>
			#cart#
		</shopping_cart>
	</inputdata>
</xtiinput>';
</cfscript>